﻿using hegang_winform.dao;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace hegang_winform.services.impl
{
    class TaskPerHour : FixedTimeTaskObject
    {
        private static DbObject o = (DbObject)Assembly.Load("hegang_winform").CreateInstance(ConfigurationManager.AppSettings["DB"]);
        public void executeTask()
        {
            DateTime now = DateTime.Now;
            //设置任务启动时间  
            double hour = Convert.ToDouble(DateTime.Now.Hour + 1);
            double minute = 0.0;   //分钟
            DateTime startTime = DateTime.Today.AddHours(hour).AddMinutes(minute);
            int delay = (int)((startTime - now).TotalMilliseconds);
            var t = new System.Threading.Timer(handle_event);
            //设置线程参数 任务delay毫秒后启动
            t.Change(delay, Timeout.Infinite);
        }
        private void handle_event(object state)
        {
            #region 执行的任务
            task1();
            task2();
            task3();
            task4();
            task5();
            task6();
            #endregion

            // 再次设定任务执行时间
            executeTask();
        }
        /// <summary>
        /// 每小时统计副井每个传感器的平均温度
        /// </summary>
        private void task1()
        {
            string str = "SELECT AVG(wd1),AVG(wd2),AVG(wd3),AVG(wd4) FROM fwd;";
            DataTable dt = o.cmdHasReturn(str);

            string timestamp = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            str = string.Format("" +
            "INSERT INTO fwd_h(wd1, wd2, wd3, wd4, timestamp) VALUES('{0}','{1}','{2}','{3}','{4}');" +
            "TRUNCATE fwd;" +
            "", dt.Rows[0][0], dt.Rows[0][1], dt.Rows[0][2], dt.Rows[0][3], timestamp);

            o.cmdNoReturn(str);
        }
        /// <summary>
        /// 每小时统计主井每个传感器的平均温度
        /// </summary>
        private void task2()
        {
            string str = "SELECT AVG(wd1),AVG(wd2),AVG(wd3),AVG(wd4) FROM mwd;";
            DataTable dt = o.cmdHasReturn(str);

            string timestamp = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            str = string.Format("" +
            "INSERT INTO mwd_h(wd1, wd2, wd3, wd4, timestamp) VALUES('{0}','{1}','{2}','{3}','{4}');" +
            "TRUNCATE mwd;" +
            "", dt.Rows[0][0], dt.Rows[0][1], dt.Rows[0][2], dt.Rows[0][3], timestamp);

            o.cmdNoReturn(str);
        }

        /// <summary>
        /// 每小时统计主井的平均残压和平均正常压力
        /// </summary>
        private void task3()
        {
            string residual_pressure = "0";
            string normal_pressure = "0";

            string str = "SELECT AVG(yl) FROM mzzyl WHERE yl < 1;";
            DataTable dt = o.cmdHasReturn(str);

            if (!string.IsNullOrEmpty(dt.Rows[0][0].ToString()))
                residual_pressure = dt.Rows[0][0].ToString();

            str = "SELECT AVG(yl) FROM mzzyl WHERE yl > 11;";
            dt = o.cmdHasReturn(str);

            if (!string.IsNullOrEmpty(dt.Rows[0][0].ToString()))
                normal_pressure = dt.Rows[0][0].ToString();

            string timestamp = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            str = string.Format("INSERT INTO mzzyl_h(residual_pressure, normal_pressure, timestamp) VALUES('{0}','{1}','{2}')", residual_pressure, normal_pressure, timestamp);
            o.cmdNoReturn(str);

            str = "TRUNCATE mzzyl;";
            o.cmdNoReturn(str);
        }
        /// <summary>
        /// 每小时统计副井的平均残压和平均正常压力
        /// </summary>
        private void task4()
        {
            string residual_pressure = "0";
            string normal_pressure = "0";

            string str = "SELECT AVG(yl) FROM fzzyl WHERE yl < 1;";
            DataTable dt = o.cmdHasReturn(str);

            if (!string.IsNullOrEmpty(dt.Rows[0][0].ToString()))
                residual_pressure = dt.Rows[0][0].ToString();

            str = "SELECT AVG(yl) FROM fzzyl WHERE yl > 11;";
            dt = o.cmdHasReturn(str);

            if (!string.IsNullOrEmpty(dt.Rows[0][0].ToString()))
                normal_pressure = dt.Rows[0][0].ToString();

            string timestamp = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            str = string.Format("INSERT INTO fzzyl_h(residual_pressure, normal_pressure, timestamp) VALUES('{0}','{1}','{2}')", residual_pressure, normal_pressure, timestamp);
            o.cmdNoReturn(str);

            str = "TRUNCATE fzzyl;";
            o.cmdNoReturn(str);
        }

        /// <summary>
        /// 统计主井振动
        /// </summary>
        private void task5()
        {
            string str = "SELECT AVG(zd1),AVG(zd2),AVG(zd3),AVG(zd4) FROM mzd;";
            DataTable dt = o.cmdHasReturn(str);

            string timestamp = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            str = string.Format("" +
                "INSERT INTO mzd_h(zd1, zd2, zd3, zd4, timestamp) VALUES('{0}','{1}','{2}','{3}','{4}');" +
                "TRUNCATE mzd;" +
                "", dt.Rows[0][0], dt.Rows[0][1], dt.Rows[0][2], dt.Rows[0][3], timestamp);
            o.cmdNoReturn(str);
        }
        /// <summary>
        /// 统计副井振动
        /// </summary>
        private void task6()
        {
            string str = "SELECT AVG(zd1),AVG(zd2),AVG(zd3),AVG(zd4) FROM fzd;";
            DataTable dt = o.cmdHasReturn(str);

            string timestamp = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            str = string.Format("" +
                "INSERT INTO fzd_h(zd1, zd2, zd3, zd4, timestamp) VALUES('{0}','{1}','{2}','{3}','{4}');" +
                "TRUNCATE fzd;" +
                "", dt.Rows[0][0], dt.Rows[0][1], dt.Rows[0][2], dt.Rows[0][3], timestamp);
            o.cmdNoReturn(str);
        }
    }
}
